//
// Created by steven on 2017/2/24.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMOrderListViewController.h"
#import "GMOrderTableViewCell.h"
#import "GMOrderDetailViewController.h"
#import "GMOrderDataController.h"
#import <MJRefresh/MJRefresh.h>

#define kOrderTableViewCellIdentifier @"kOrderTableViewCellIdentifier"

@interface GMOrderListViewController()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *orders;
@property (nonatomic, strong)GMOrderDataController *orderDataController;

@end

@implementation GMOrderListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.orderDataController = [[GMOrderDataController alloc] init];
    self.orders = [[NSMutableArray alloc] init];

    [self setupMainView];
    [self setMJRefresh];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self refreshOrders];
}

- (void)setupMainView {
    self.navigationItem.title = @"我的订单";
    [self.view addSubview:self.tableView];
    [self.tableView autoPinEdgesToSuperviewEdges];
}

#pragma mark - set MJRefresh
- (void)setMJRefresh {

    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if(weakSelf) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf refreshOrders];
        }
    }];

    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (weakSelf) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf loadMoreOrders];
        }
    }];
    self.tableView.mj_footer.automaticallyHidden = YES;
}

#pragma mark - refresh combos
- (void)refreshOrders {
    NSDictionary *params = @{@"page":@"1", @"perpage":PER_PAGE_RECORD};
    [self requestOrdersWithParams:params];
}

#pragma mark - load more combos
- (void)loadMoreOrders {
    NSInteger currentPage = self.orderDataController.comboRecord.currentPage;
    NSInteger totalPage = self.orderDataController.comboRecord.totalPage;
    if (currentPage < totalPage) {
        NSDictionary *params = @{@"page":[NSString stringWithFormat:@"%ld", currentPage+1],@"perpage":PER_PAGE_RECORD};
        [self requestOrdersWithParams:params];
    } else {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
}

#pragma mark - request combos
- (void)requestOrdersWithParams:(NSDictionary *)params {
    [self.orderDataController requestOrdersWithParams:params callback:^(NSString *error) {
        if (error == nil) {
            [self renderSuccessView];
        } else {
            [self showAlertMessage:error];
        }
    }];
}

- (void)renderSuccessView {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];

    self.orders = self.orderDataController.orderRecord.orders;
    [self.tableView reloadData];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[GMOrderTableViewCell class] forCellReuseIdentifier:kOrderTableViewCellIdentifier];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.orders.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GMOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kOrderTableViewCellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[GMOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kOrderTableViewCellIdentifier];
    }

    GMOrderModel *orderModel = self.orders[indexPath.row];

    cell.countLabel.text = orderModel.count;
    cell.dateLabel.text = orderModel.buyTime;
    cell.costLabel.text = [NSString stringWithFormat:@"价格 %@", orderModel.price];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 84;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    GMOrderModel *orderModel = self.orders[indexPath.row];
    
    GMOrderDetailViewController *detailViewController = [[GMOrderDetailViewController alloc] init];
    detailViewController.orderModel = orderModel;
    [self.navigationController pushViewController:detailViewController animated:YES];
}


@end
